package com.exam.bean.privilege;

import java.io.Serializable;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import com.exam.bean.Sex;
import com.exam.bean.UseRole;
import com.exam.bean.paper.AnswerPaper;
import com.exam.bean.paper.Paper;

/**
 * Ա用户
 *
 */
@Entity
public class Employee implements Serializable{
	
	private static final long serialVersionUID = 2174717956060716427L;
	private String username;//用户名
	private String password;//密码
	private String realname;//名字
	private Set<Department> departments = new HashSet<Department>();//所属组织结构
	private IDCard idcard;//身份证
	private String phone;//联系电话
	private String email;//电子邮件
	private Sex sex;// 性别
	private UseRole usercate;//考生or教师
	private String picture;//照片
	private Date createdate = new Date();//入职时间
	private String degree;//学历
	private Boolean visible = true;//用户在职状态,true为在职,false为离职
	private Set<PrivilegeGroup> privilegegroups = new HashSet<PrivilegeGroup>();//所在权限组
	private Set<Paper> papers=new HashSet<Paper>();//教师的卷子 
	private Set<Paper> studentpapers=new HashSet<Paper>();//考生的卷子
	private Set<AnswerPaper> answerpapers=new HashSet<AnswerPaper>();//教师的改卷题
	private Set<AnswerPaper> studentanswerpapers=new HashSet<AnswerPaper>();//考生的卷子答题
	
	public Employee(){}
	
	public Employee(String username){
		this.username = username;
	}
	
	public Employee(String username, String password, String realname,
			IDCard idcard, String phone, String email, Sex sex, String degree) {
		this.username = username;
		this.password = password;
		this.realname = realname;
		this.idcard = idcard;
		this.phone = phone;
		this.email = email;
		this.sex = sex;
		this.degree = degree;
	}
	
	
	@Id @Column(length=32)
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	
	@Column(length=32,nullable=false)
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Column(length=10,nullable=false)
	public String getRealname() {
		return realname;
	}
	public void setRealname(String realname) {
		this.realname = realname;
	}
	
	@ManyToMany(cascade=CascadeType.REFRESH, fetch=FetchType.EAGER)
	@JoinTable(name="employee_department", joinColumns=@JoinColumn(name="username"),
			inverseJoinColumns=@JoinColumn(name="department_id"))
	public Set<Department> getDepartments() {
		return departments;
	}
	public void setDepartments(Set<Department> departments) {
		this.departments = departments;
	}
	
	@OneToOne(cascade=CascadeType.ALL,optional=false)
	@JoinColumn(name="card_id")
	public IDCard getIdcard() {
		return idcard;
	}
	public void setIdcard(IDCard idcard) {
		this.idcard = idcard;
	}
	
	@Column(length=18)
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}
	
	@Column(length=50)
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	
	@Enumerated(EnumType.STRING) @Column(nullable=false,length=5)
	public Sex getSex() {
		return sex;
	}
	public void setSex(Sex sex) {
		this.sex = sex;
	}
	
	@Enumerated(EnumType.STRING) @Column(nullable=false,length=8)
	public UseRole getUsercate() {
		return usercate;
	}
	public void setUsercate(UseRole usercate) {
		this.usercate = usercate;
	}

	@Column(length=80)
	public String getPicture() {
		return picture;
	}
	public void setPicture(String picture) {
		this.picture = picture;
	}
	
	@Temporal(TemporalType.DATE)
	public Date getCreatedate() {
		return createdate;
	}
	public void setCreatedate(Date createdate) {
		this.createdate = createdate;
	}
	
	@Column(length=5)
	public String getDegree() {
		return degree;
	}
	public void setDegree(String degree) {
		this.degree = degree;
	}
	
	@Column(nullable=false)
	public Boolean getVisible() {
		return visible;
	}
	public void setVisible(Boolean visible) {
		this.visible = visible;
	}
	
	@ManyToMany(cascade=CascadeType.REFRESH,fetch=FetchType.EAGER)
	@JoinTable(name="employee_privilegegroup",joinColumns=@JoinColumn(name="username"),
			inverseJoinColumns=@JoinColumn(name="group_id"))
	public Set<PrivilegeGroup> getPrivilegegroups() {
		return privilegegroups;
	}
	public void setPrivilegegroups(Set<PrivilegeGroup> privilegegroups) {
		this.privilegegroups = privilegegroups;
	}

	@OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="employee")
	public Set<Paper> getPapers() {
		return papers;
	}
	public void setPapers(Set<Paper> papers) {
		this.papers = papers;
	}
	
	@ManyToMany(cascade=CascadeType.REFRESH, fetch=FetchType.EAGER)
	@JoinTable(name="employee_paper", joinColumns=@JoinColumn(name="username"),
			inverseJoinColumns=@JoinColumn(name="paper_id"))
	public Set<Paper> getStudentpapers() {
		return studentpapers;
	}
	public void setStudentpapers(Set<Paper> studentpapers) {
		this.studentpapers = studentpapers;
	}

	@OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="employee")
	public Set<AnswerPaper> getAnswerpapers() {
		return answerpapers;
	}
	public void setAnswerpapers(Set<AnswerPaper> answerpapers) {
		this.answerpapers = answerpapers;
	}

	@OneToMany(cascade={CascadeType.REFRESH,CascadeType.REMOVE},mappedBy="student")
	public Set<AnswerPaper> getStudentanswerpapers() {
		return studentanswerpapers;
	}
	public void setStudentanswerpapers(Set<AnswerPaper> studentanswerpapers) {
		this.studentanswerpapers = studentanswerpapers;
	}

	public void addDepartment(Department department){
		if(!this.departments.contains(department)) this.departments.add(department);
	}
	
	@Transient
	public String getPicturePath(){
		return "/images/employee/"+ username+ "/"+ picture;
	}
	
	public void addPrivilegegroup(PrivilegeGroup privilegegroup){
		if(!this.privilegegroups.contains(privilegegroup)) this.privilegegroups.add(privilegegroup);
	}

	
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((username == null) ? 0 : username.hashCode());
		return result;
	}
	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		final Employee other = (Employee) obj;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
}
